package Week.Week317;

import java.util.LinkedList;

 public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;

     TreeNode() {
     }

     TreeNode(int val) {
         this.val = val;
     }

     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }

public class p4 {
    class Solution {
        public int[] treeQueries(TreeNode root, int[] queries) {
            int[] ans = new int[queries.length];
            for(int i = 0; i < queries.length; i++){
                LinkedList<TreeNode> queue = new LinkedList<>();
                queue.add(root);
                int h = -1;
                while (!queue.isEmpty()) {
                    int len = queue.size();
                    h++;
                    for (int j = 0; j < len; j++) {
                        TreeNode node = queue.poll();
                        if (node.left != null && node.left.val != queries[i]) queue.add(node.left);
                        if (node.right != null && node.right.val != queries[i]) queue.add(node.right);
                    }
                }
                ans[i] = h;
            }
            return ans;
        }
    }
}
